#include "Mutex.hpp"
#include <string>
#include <unistd.h>

int ticket = 1000;

wgm::Mutex lock;

void *routine(void *arg)
{
    std::string name = static_cast<char *>(arg);
    while (true)
    {   
        wgm::MutexGuard guard_lock(lock);
        if (ticket > 0)
        {
            usleep(1000);
            std::cout << name << " 当前票数：" << --ticket << std::endl;
        }
        else
        {
            std::cout << "票已经抢完了！" << std::endl;
            break;
        }
    }
    return nullptr;
}

int main()
{

    pthread_t t1, t2, t3, t4;
    pthread_create(&t1, nullptr, routine, (void *)"thread - 1");
    pthread_create(&t2, nullptr, routine, (void *)"thread - 2");
    pthread_create(&t3, nullptr, routine, (void *)"thread - 3");
    pthread_create(&t4, nullptr, routine, (void *)"thread - 4");

    pthread_join(t1, nullptr);
    pthread_join(t2, nullptr);
    pthread_join(t3, nullptr);
    pthread_join(t4, nullptr);
    return 0;
}